package com.loyal.mwm.ui

import android.content.DialogInterface
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.view.View
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import com.loyal.core.helper.DialogHelper
import com.loyal.mwm.R
import com.loyal.mwm.base.BaseActivity
import com.loyal.mwm.databinding.ActivitySplashBinding
import com.loyal.mwm.util.ImageUtil
import com.loyal.mwm.util.MemoryUtil

class SplashActivity : BaseActivity<ActivitySplashBinding>() {

    private val runnable = SplashRunnable(3)
    private val stringBuilder = StringBuilder()
    private var permissionLauncher: ActivityResultLauncher<Array<String>>? = null

    override fun isFullScreen(): Boolean {
        return true
    }

    override fun actLayoutRes(): Int {
        return R.layout.activity_splash
    }

    override fun afterOnCreate() {
        binding.drawable = ImageUtil.getBackground(this)
        binding.skip.setOnClickListener {
            skip()
        }
        permissionLauncher =
            registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {
                if (it.containsValue(false)) {
                    val messageDialog = DialogHelper.createMessageDialog(this, "请开启$stringBuilder")
                    messageDialog?.setCancelable(false)
                    messageDialog?.setCanceledOnTouchOutside(false)
                    messageDialog?.setButton(
                        DialogInterface.BUTTON_POSITIVE,
                        "确定"
                    ) { dialog, _ ->
                        dialog?.dismiss()
                        startActivity(Intent(Settings.ACTION_SETTINGS)) //直接进入手机中设置界面
                        finish()
                    }
                    messageDialog?.show()
                    return@registerForActivityResult
                }
                MemoryUtil.mkPkgDirs()
                binding.skip.visibility = View.VISIBLE
                binding.skip.text = String.format(getString(R.string.skip), 3)
                binding.skip.postDelayed(runnable, 1000L)
            }
    }

    override fun onPostCreate(savedInstanceState: Bundle?) {
        super.onPostCreate(savedInstanceState)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            PERMISSIONS.remove(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
        }
        permissionLauncher?.launch(PERMISSIONS.toTypedArray())
    }

    private fun skip() {
        binding.skip.removeCallbacks(runnable)
        val intent = Intent(this, LoginActivity::class.java)
        startActivity(intent)
        finish()
    }

    private inner class SplashRunnable(private var mWhat: Int) : Runnable {
        override fun run() {
            if (mWhat == 1) {
                skip()
                return
            }
            binding.skip.text = String.format(getString(R.string.skip), --mWhat)
            binding.skip.postDelayed(this, 1000L)
        }
    }

    override fun onDestroy() {
        binding.skip.removeCallbacks(runnable)
        super.onDestroy()
    }

    companion object {
        private val PERMISSIONS = mutableListOf(
            android.Manifest.permission.READ_PHONE_STATE,
            android.Manifest.permission.READ_EXTERNAL_STORAGE,
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE
        )
    }
}